﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
        public string DBStr = "Data Source=isostore:Data/PhoneDB/mintexdb.sdf";
        private MINTEXDB _LocalDB = null;
        public MINTEXDB LocalDB
        {
            get
            {
                if (_LocalDB == null)
                {
                    _LocalDB = new MINTEXDB(DBStr);
                    /*if (!System.IO.File.Exists(_LocalDB.Connection.DataSource))
                    {
                        _LocalDB.CreateDatabase();
                    }*/
                }
                return _LocalDB;
            }
            set
            {
                _LocalDB = value;
            }
        }
        public object Map(string file, bool mergedb, string dbstr = "Data Source=isostore:Data/PhoneDB/mintexdb.sdf")
        {
            object result = null;
            StreamReader sw = null;
            DBStr = dbstr;
            IsolatedStorageFile isofile = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isofile.FileExists(file))
            {
                return result;
            }
            sw = new StreamReader(isofile.OpenFile(file, FileMode.Open, FileAccess.Read));
            XDocument xdoc = XDocument.Load(sw);
            if (sw != null)
            {
                sw.Close();
                sw.Dispose();
            }
            result = MapFinal(xdoc, mergedb);
            return result;
        }
        public object MapFinal(XDocument xdoc, bool mergedb)
        {
            
            object result = null;
            if (xdoc == null)
                return result;
            var item1 = from item in xdoc.Elements()
                        select item;
            List<XElement> clist = item1.ToList<XElement>();
            string table = "";
            foreach (XElement item2 in clist)
            {
                table = item2.Name.ToString();
                #region======Converter==========
                switch (table.ToUpper())
                {
                    //"GSDAT;LIEFDAT;WASTAMM;GPBST;WBDAT;VAFARBEN;VAART;KDSTAMM;VKPOS;VKKOPF;WAFARBEN;
                    //WDECK;ABAUFM;ABPOS;ABFOLGE;BAAUF;GAAUF;GSCOLOR;";
                    case "LOGINFO":
                        result = MapLOGINFO(item2, mergedb);
                        break; 
                    case "SKYFILE":
                        result = MapSKYFILE(item2, mergedb);
                        break;
                    case "SKYDRIVEFOLDER":
                        result = MapSKYFILE(item2, mergedb);
                        break;
                    case "GSDAT":
                        result = MapGSDAT(item2, mergedb);
                        break;
                    case "LIEFDAT":
                        result = MapLIEFDAT(item2, mergedb);
                        break;
                    case "WASTAMM":
                        result = MapWASTAMM(item2, mergedb);
                        break;
                    case "GPBST":
                        result = MapGPBST(item2, mergedb);
                        break;
                    case "WBDAT":
                        result = MapWBDAT(item2, mergedb);
                        break;
                    case "VAFARBEN":
                        result = MapVAFARBEN(item2, mergedb);
                        break;
                    case "VAART":
                        result = MapVAART(item2, mergedb);
                        break;
                    case "VAKOLL":
                        result = MapVAKOLL(item2, mergedb);
                        break;
                    case "VKKOPF":
                        result = MapVKKOPF(item2, mergedb);
                        break;
                    case "WAFARBEN":
                        result = MapWAFARBEN(item2, mergedb);
                        break;
                    case "WDECK":
                        result = MapWDECK(item2, mergedb);
                        break;
                    case "ABAUFM":
                        result = MapABAUFM(item2, mergedb);
                        break;
                    case "ABFOLGE":
                        result = MapABFOLGE(item2, mergedb);
                        break;
                    case "BAAUF":
                        result = MapBAAUF(item2, mergedb);
                        break;
                    case "GAAUF":
                        result = MapGAAUF(item2, mergedb);
                        break;
                    case "GSCOLOR":
                        result = MapGAAUF(item2, mergedb);
                        break;
                    case "ABPOS":
                        result = MapABPOS(item2, mergedb);
                        break;
                    case "VKPOS":
                        result = MapVKPOS(item2, mergedb);
                        break;
                    case "KDSTAMM":
                        result = MapKDSTAMM(item2, mergedb);
                        break;
                    case "WPMENU":
                        result = MapWPMENU(item2, mergedb);
                        break;
                    case "VKPOS_EXT":
                        result = MapVKPOS_EXT(item2, mergedb);
                        break;
                    case "VKKOPF_EXT":
                        result = MapVKKOPF_EXT(item2, mergedb);
                        break;
                    default:
                        break;
                }
                #endregion
            }

            return result;
        }
        
    } 
}
